package com.site.wisdom_construction.controller.qualityModule;

import com.site.wisdom_construction.responseEntity.JsonResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.service.qualityModule.QualityInspectionService;
import com.site.wisdom_construction.util.imgUtil.CodeImgUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author daiyifei
 * @Date 2022/1/12
 * @Description
 */
@Api(tags = "巡检点信息接口")
@RestController
@RequestMapping("QualityInspection")
@CrossOrigin
public class QualityInspectionController {
    @Autowired
    private QualityInspectionService qualityInspectionService;

    @ApiOperation(value = "增加巡检点信息", notes = "测试数据:")
    @PostMapping("/insertQualityInspection")
    public JsonResult insertQualityInspection(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();

        //二维码工具类
        CodeImgUtil codeImgUtil = new CodeImgUtil();
        //二维码内容
        String content = (String) map.get("inspectionName");
        //二维码图片地址
        String inspectionCode = codeImgUtil.CodeImgUtil(content);
        if (("创建失败").equals(inspectionCode)){
            jsonResult.setCode(20005);
            jsonResult.setMessage("二维码生成失败!");
            return jsonResult;
        }
        map.put("inspectionCode",inspectionCode);
        int i = qualityInspectionService.insertQualityInspection(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功录入巡检点信息!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("录入巡检点信息失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "删除巡检点信息信息", notes = "测试数据:")
    @PostMapping("/deleteQualityInspection")
    public JsonResult deleteQualityInspection(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        List<Integer> list = (List<Integer>) map.get("list");
        int l = 0;
        Map mapR = new HashMap();
        for (int i = 0;i<list.size();i++){
            mapR.put("id",list.get(i));
            int a = qualityInspectionService.deleteQualityInspection(mapR);
            l+=a;
        }
        if (l>=1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功删除巡检点信息!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除巡检点信息失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改巡检点信息", notes = "测试数据:")
    @PostMapping("/updateQualityInspection")
    public JsonResult updateQualityInspection(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();

        //二维码工具类
        CodeImgUtil codeImgUtil = new CodeImgUtil();
        //二维码内容
        String content = (String) map.get("inspectionName");
        //二维码图片地址
        String inspectionCode = codeImgUtil.CodeImgUtil(content);
        if (("创建失败").equals(inspectionCode)){
            jsonResult.setCode(20005);
            jsonResult.setMessage("二维码修改失败!");
            return jsonResult;
        }
        map.put("inspectionCode",inspectionCode);

        int i = qualityInspectionService.updateQualityInspection(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功修改巡检点信息!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("修改巡检点信息失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页模糊查询巡检点信息", notes = "测试数据:")
    @PostMapping("/findQualityInspectionByPage")
    public Page findQualityInspectionByPage(@RequestBody Map map) {
        Page page = new Page();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(qualityInspectionService.Total(map));
        page.setItems(qualityInspectionService.findQualityInspectionByPage(map));
        return page;
    }
}
